﻿namespace JumpstartApp.Models
{
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    using JumpstartApp.Helpers;

    using Newtonsoft.Json;

    using Windows.Storage;

    public class EpisodeNotes
    {
        #region Constants

        private const string SavedNotesFilename = "SavedNotes.txt";

        #endregion

        #region Static Fields

        private static EpisodeNotes instance;

        #endregion

        #region Fields

        private readonly Dictionary<string, string> episodeNotes;

        private static SkyDriveHelper.SkyDriveFolderInfo skyDriveAppDataFolder;

        #endregion

        #region Constructors and Destructors

        private EpisodeNotes(string notesAsJson)
        {
            this.episodeNotes = JsonConvert.DeserializeObject<Dictionary<string, string>>(notesAsJson);
        }

        #endregion

        #region Public Indexers

        public string this[string sessionId]
        {
            get
            {
                if (!this.episodeNotes.ContainsKey(sessionId))
                {
                    this.episodeNotes[sessionId] = string.Empty;
                }

                return this.episodeNotes[sessionId];
            }

            set
            {
                if (!this.episodeNotes.ContainsKey(sessionId))
                {
                    this.episodeNotes.Add(sessionId, value);
                }

                this.episodeNotes[sessionId] = value;
            }
        }

        #endregion

        #region Public Methods and Operators

        public static async Task<EpisodeNotes> GetNotes()
        {
            if (instance != null)
            {
                return instance;
            }

            var json = "{}";

            try
            {
                skyDriveAppDataFolder = await SkyDriveHelper.GetOrCreateApplicationDataFolder(App.AppNameForSkyDrive);

                var notesFromSkyDriveFileInfo =
                    await SkyDriveHelper.GetFileIfExists(SavedNotesFilename, skyDriveAppDataFolder.Id);
                
                if (notesFromSkyDriveFileInfo != null)
                {
                    var localNotesFromSkyDriveFile =
                        await
                        ApplicationData.Current.TemporaryFolder.CreateFileAsync(
                            SavedNotesFilename, CreationCollisionOption.ReplaceExisting);

                    if (await SkyDriveHelper.DownloadFile(notesFromSkyDriveFileInfo.DownloadPath, localNotesFromSkyDriveFile))
                    {
                        json = await FileIO.ReadTextAsync(localNotesFromSkyDriveFile);
                    }
                }
            }
            catch 
            {
            }

            instance = new EpisodeNotes(json);
            return instance;
        }

        public bool Contains(string sessionId)
        {
            return this.episodeNotes.ContainsKey(sessionId);
        }

        public async Task Save()
        {
            var json = JsonConvert.SerializeObject(this.episodeNotes);
            var localNotesToSkyDriveFile =
                await
                ApplicationData.Current.TemporaryFolder.CreateFileAsync(
                    SavedNotesFilename, CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteTextAsync(localNotesToSkyDriveFile, json);
            await SkyDriveHelper.UploadFile(localNotesToSkyDriveFile, SavedNotesFilename, skyDriveAppDataFolder.Id);
        }

        #endregion
    }
}